﻿using INTEX.DataBase;
using INTEX.DataBase.Tools;
using ReflectionStudio.Core;
using ReflectionStudio.Core.Events;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.IO;
using System.Linq;
using System.Windows;
namespace WPFExcelReport
{
    public partial class ITXEXRPT
    {
        public void YGRGBMOTHLY(DataSet db)
        {
            int x = 4, y = 1;
            string VKPPOSTYP = "";
            DataTable dtOutput = new DataTable();
            try
            {

                #region======Title===========
                int id = 1;
                exAgent.SetCellValue(3, id++, "LO"); dtOutput.Columns.Add("LO", typeof(System.String));
                exAgent.SetCellValue(3, id++, "LB"); dtOutput.Columns.Add("LB", typeof(System.String));
                exAgent.SetCellValue(3, id++, "纱型"); dtOutput.Columns.Add("FBPART", typeof(System.String));
                exAgent.SetCellValue(3, id++, "色号"); dtOutput.Columns.Add("FARBE", typeof(System.String));
                exAgent.SetCellValue(3, id++, "批号"); dtOutput.Columns.Add("SPPART", typeof(System.String));
                exAgent.SetCellValue(3, id++, "纱线"); dtOutput.Columns.Add("ART", typeof(System.String));
                exAgent.SetCellValue(3, id++, "入库日期"); dtOutput.Columns.Add("JP90", typeof(System.String));
                exAgent.SetCellValue(3, id++, "打包带"); dtOutput.Columns.Add("BEM1", typeof(System.String));
                exAgent.SetCellValue(3, id++, "期初库存"); dtOutput.Columns.Add("BS00", typeof(System.Double));
                exAgent.SetCellValue(3, id++, "期末库存"); dtOutput.Columns.Add("STOCKQTY", typeof(System.Double));
                exAgent.SetCellValue(3, id++, "入库"); dtOutput.Columns.Add("BS02", typeof(System.Double));
                exAgent.SetCellValue(3, id++, "销售出库"); dtOutput.Columns.Add("BS20", typeof(System.Double));
                exAgent.SetCellValue(3, id++, "盘亏"); dtOutput.Columns.Add("BS42", typeof(System.Double));
                #endregion

                #region======ConvertData===
                #endregion

                var qGW = from GW in db.Tables["ICC_STOCKM"].AsEnumerable()
                          group GW by new
                          {
                              PT = GW["MDATE"].ToString(),
                          }
                              into g
                              select new
                              {
                                  KEY = g.Key,
                              };
                foreach (var GW in qGW)
                {
                    string MDATE = GW.KEY.PT;
                    exAgent.SetCellValue(1, 3, "成品仓库" + Helpers.Helper.Left(MDATE, 4) + "年" + Helpers.Helper.Right(MDATE, 2) + ")月报"+Environment.NewLine+date);
                    exAgent.RenameSheet(date);
                    DataRow[] rows = db.Tables["ICC_STOCKM"].Select();
                    DataTable dtSoruce = rows.CopyToDataTable();
                    dtOutput.Clear();
                    dtOutput.Merge(dtSoruce, false, MissingSchemaAction.Ignore);
                    foreach (DataRow rx in dtOutput.Rows)
                    {
                        rx["BEM1"] = GetGB2312Value(rx["BEM1"].ToString());
                    }
                    #region====Excel==========
                    int count = dtOutput.Rows.Count;
                    exAgent.RangeCopy(4, 50, 5, 4 + count);
                    exAgent.DataTableToExcel(dtOutput, x, 1);
                    //MergeSame(x, count);
                    exAgent.SetCellValue(1, 1, 1);
                    exAgent.BatchConvert2Number(4, 9, count + 3, 13);
                    exAgent.SetCellValue(1, 1, " ");
                    #endregion
                    break;
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            finally
            {
                EventDispatcher.Instance.RaiseStatus("完成", StatusEventType.StopProgress);
            }
        }

    }
}